package jnpf.engine.custom.cmd.multi;


import jnpf.engine.util.SpringContextHolder;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;

import java.util.List;

import static org.flowable.common.engine.impl.interceptor.EngineConfigurationConstants.KEY_PROCESS_ENGINE_CONFIG;

/**
 * @author LoveMyOrange
 * @create 2022-01-26 13:43
 */
public class SimpleUserTaskSignCmd implements Command<Void> {
    protected  String taskId;
    protected List<String> candidateUsers;
    protected String operator;

    public SimpleUserTaskSignCmd(String taskId, List<String> candidateUsers, String operator) {
        this.taskId = taskId;
        this.candidateUsers = candidateUsers;
        this.operator = operator;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        SpringProcessEngineConfiguration springProcessEngineConfiguration = SpringContextHolder.getBean(SpringProcessEngineConfiguration.class);
        TaskEntityManager taskEntityManager = springProcessEngineConfiguration.getTaskServiceConfiguration().getTaskEntityManager();
        TaskEntity taskEntity = taskEntityManager.findById(taskId);
        if("add".equals(operator)){
            taskEntity.addCandidateUsers(candidateUsers);
        }
        else{
            for (String candidateUser : candidateUsers) {
                taskEntity.deleteCandidateUser(candidateUser);
            }
        }
        return null;
    }
}
